﻿using App.DbContexts;
using App.UnitOfWork;

namespace App.Business
{
    /// <summary>
    /// 这种是不使用注入的方式写出来的，是可行的。
    /// 正规使用，请查看TestBusiness2.cs
    /// </summary>
    public class TestBusiness
    {
        public void Add()
        {
            //处理业务，判断等

            //构建实体
            var testModel = new TestEntity { Name1 = "1", Name2 = "2" };
            var testModel2 = new TestEntity { Name1 = "1", Name2 = "2" };


            using (var _unitOfWork = new UnitOfWork<AppTestDBContext>())
            {
                var testRepository = _unitOfWork.GetRepository<TestEntity>();

                //添加
                testRepository.Insert(testModel);
                testRepository.Insert(testModel2);
                _unitOfWork.SaveChanges();//持久化
            }
        }

        public void Query()
        {
            using (var _unitOfWork = new UnitOfWork<AppTestDBContext>())
            {
                var testRepository = _unitOfWork.GetRepository<TestEntity>();

                //获取列表
                var list = testRepository.QueryAble().ToList();

                //根据id查询
                var first = testRepository.Find(1);

                //获取IQueryAble
                var query = testRepository.QueryAble();

                //获取分页(自行拓展改造)
                //var page=testRepository.GetPagedList()
            }
        }

        public void Delete()
        {
            using (var _unitOfWork = new UnitOfWork<AppTestDBContext>())
            {
                var testRepository = _unitOfWork.GetRepository<TestEntity>();

                //根据id查询
                var first = testRepository.Find(1);
                testRepository.Delete(first);//根据实体删除

                testRepository.Delete(2);//根据id删除
                _unitOfWork.SaveChanges();//持久化

            }

        }

        public void Transaction()
        {
            using (var _unitOfWork = new UnitOfWork<AppTestDBContext>())
            {
                var testRepository = _unitOfWork.GetRepository<TestEntity>();
                var testDetailRepository = _unitOfWork.GetRepository<TestDetailEntity>();

                //添加
                using (var tran = _unitOfWork.BeginTransaction())
                {
                    try
                    {

                        var test = testRepository.Insert(new TestEntity() { Name1 = "1", Name2 = "2" });
                        var detail1 = new TestDetailEntity() { TestId = test.Id, Detail1 = "Detail1a", Detail2 = "Detail2a" };
                        var detail2 = new TestDetailEntity() { TestId = test.Id, Detail1 = "Detail1b", Detail2 = "Detail2b" };
                        var testDetail1 = testDetailRepository.Insert(detail1);
                        var testDetail2 = testDetailRepository.Insert(detail2);
                        tran.Commit();
                        _unitOfWork.SaveChanges();//持久化
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        throw;
                    }
                }
            }

        }
    }
}